<?php
// 本类由系统自动生成，仅供测试用途
   
class MemberAction extends CommonAction{
	var $url = "__APP__"; 
	
	public function index() { 
		  //echo "建设中";
	}	
	public function chk(){
		if(Cookie::get('acc_name')==""){
			$this->assign('jumpUrl',"__APP__/Public/login"); 
			$this->error("请登录!");
			return false;
		}		
	}
	
	public function ucenter(){
		$this->chk();
		$acc_name = Cookie::get('acc_name');
		$user = d('member_info')->where("acc_name = '".$acc_name."'")->find();
		$this->gchk();
		$this->assign('user',$user);	
		$this->display("member:uc_index");
	
	}
	
	public function uc_edit(){
		
		if(Cookie::get('acc_name')!=""){
			
			if($_POST['edit_btn']){
				$user_arr['acc_name'] = $_POST['acc_name'];
				$user_arr['pwd'] = md5($_POST['pwd']);
				
				$user = d("member_info");
				$checkUser = $user->where($user_arr)->find();
				if($checkUser){
					$user_arr['email'] = $_POST['email'];
					$user_arr['province'] = $_POST['pro_id'];
					$user_arr['city'] = $_POST['city_id'];
					$user->where(array('acc_name'=>$user_arr['acc_name']))->save($user_arr);
					$this->ajaxReturn(__ROOT__."/index.php/member/ucenter","修改成功！",1);
				}
				else{
					$this->ajaxReturn(__ROOT__."/index.php/member/ucenter","用户名密码错误！",0);
				}

			
			
			}
			else{
				$acc_name = Cookie::get('acc_name');
				$this->assign('pro_arr',d("province")->order("prov_id asc")->findall());	
				$this->assign('city_arr',d("city")->order("city_id asc")->findall());	
				$user = d('member_info')->where("acc_name = '".$acc_name."'")->find();
				
				$this->gchk();
				$this->assign('mem_arr',$user);	
				$this->assign('uid',$user['uid']);	
				$this->display();
			}
		}
		else{
			$this->assign('jumpUrl',"__APP__/Public/login"); 
			$this->success("请登录!");
		}
	}
	
	public function character(){
		$this->chk();			
		$uid = $_GET['uid'];
		$uname = Cookie::get('acc_name');
		
		if($_POST['char_add']){
			if($_POST['char_name'] && $_POST['server'] ){
				$char['acc_name'] = $uname;
				$char['char_name'] = $_POST['char_name'];
				$char['server'] = $_POST['server'];
				
				if(d('character')->where($char)->find()){
					$this->assign('jumpUrl',"__URL__/ucenter/character");
					$this->error('该角色已经被添加过!');
				}
				else{
					$char['guild'] = $_POST['guild_name'];	
					if(d('character')->add($char)){
					$this->assign('jumpUrl',"__URL__/ucenter/character");
					$this->success('添加成功!');
					}
				}
			}
			$this->assign('jumpUrl',"__URL__/ucenter/character");
			$this->error('角色名/服务器名提交失败!');
		}
		else{
			if($uname){
				$character = d('character')->where(array("acc_name" => $uname))->findall();
				//dump($character);
			}			
			if ($character){

				foreach($character as $char){			
					$xml = $this->getXML( $char['server'], $char['char_name'] );
					$c_array = (array)$xml->characterInfo->character;
					$char_arr[] = array_merge($c_array['@attributes'],array('Signature'=>$char['Signature']));
					
								
					foreach($xml->characterInfo->characterTab->items->item as $v){
						
						$item = (array)$v;
						$item_url = $armory_server . 'item-info.xml?i=' . $item["@attributes"]['id'];
						$item_arr[$item["@attributes"]['id']]['id'] = $item["@attributes"]['id'];
						$item_arr[$item["@attributes"]['id']]['icon'] = $item["@attributes"]['icon'];
						$item_name = (array)($result->itemInfo->item);
	  					$item_arr[$item["@attributes"]['id']]['backimage'] = 'http://db.178.com/wow/icons/m/' . $item["@attributes"]['icon'] . '.jpg';
					}
					//$char_arr[] = $xml->characterInfo->character['name'];
					//$re = array_merge($item_arr,$c_array);
					$item_result[$c_array['@attributes']['name']] = $item_arr;
					$item_arr = "";
					
				}	
				
				$this->assign('item_arr',$item_result);
				$this->assign('char',$char_arr);
				$this->assign("display","block");
				
			}
			else{
				$this->assign("msg","还未绑定角色");
				$this->assign("display","none");
			}
			//dump($item_result);
			//dump($char_arr);
			$this->gchk();
			$this->assign("uname",$uname);
			$this->display();
		}
		
	}
	
	
	public function char_del(){
		$this->chk();
		$char['acc_name'] = Cookie::get('acc_name');
		$char['char_name'] = $_GET['char_name'];
		$msg = "角色删除失败！";
		$this->assign('jumpUrl',"__URL__/ucenter"); 
		if($char){
			if(d('character')->where($char)->delete()){
				$msg = "角色已删除！";
				$this->success($msg);
				exit;
			}
		}
		$this->error($msg);
	}
	
	public function char_edit(){
		$this->chk();
		$char['acc_name'] = Cookie::get('acc_name');
		if($_POST['char_name']){
			d('character')->where($char)->save(array('Signature'=>0));
			
			$char['char_name'] = $_POST['char_name'];
			$map = $char;	
			if(d('character')->where($map)->save(array('Signature'=>1))){
					$this->ajaxReturn("",'修改成功!',1);				
			}	
		}
		else{
			$this->ajaxReturn("",'修改失败!',0);				
		}
		
	}
	
	public function char_query(){
		//echo "char_query";
		$char['char_name'] = $_POST['char_name'];
		$char['server'] = $_POST['server'];
		
		if(!empty($char['char_name']) && !empty($char['server'])){
			$xml = $this->getXML($char['server'],$char['char_name']);			
			//dump($xml);
						if($xml->characterInfo['errCode'] ||$xml->errorhtml ){
				$this->ajaxReturn("",'没有找到角色!',0);			
			}
			else{
				$cha_info = (array)$xml->characterInfo->character;
				$char_array = $cha_info['@attributes'];	
				$this->ajaxReturn($char_array,'找到角色!',1);				
			}
		}
		else{
			$this->ajaxReturn("",'角色名/服务器名不能为空!',0);				
		}
		
	}
	
	public function itemquery(){
		$item_id = $_GET['item_id'];
		//echo $item_id;
		if($item_id){
			$xml = $this->getXML("",$item_id,"item");
			
			$this->ajaxReturn($xml,'',1);				
		}
		
		
	}
	
	
	
	public function getXML( $server,$name, $type="char",$lang="cn") {
		
		
		
			if ($lang == 'de')
				$header[] = 'Accept-Language: de-de';
			elseif ($lang == 'en')
				$header[] = 'Accept-Language: en-gb';
			elseif ($lang == 'cn')
				$header[] = 'Accept-Language: zh-cn';
			elseif ($lang == 'us')
				$header[] = 'Accept-Language: en-us';
			$browser = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) Firefox/3.6';
			
		if($type=="char"){	
			$armory_server = "http://$lang.wowarmory.com/";
			$armory_xml_file = 'character-sheet.xml';
			$url =  $armory_server. $armory_xml_file . '?r=' . urlencode($server) . '&cn=' . urlencode($name);
			
		}
		elseif($type=="item"){
			$url = 'http://cn.wowarmory.com/item-tooltip.xml?i='.$name;
			
		}
		elseif($type=="guild"){
			$url = 'http://cn.wowarmory.com/character-sheet.xml?r='.$server. '&gn=' . $name;
			
			
		}
		else{
			return false;
		}
		//echo $url;
		$ch = curl_init();		
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header ); 
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 15 );
		curl_setopt ( $ch, CURLOPT_USERAGENT, $browser );
		$url_string = curl_exec( $ch );
		curl_close( $ch );
		//echo $url_string;
		return simplexml_load_string($url_string );
		//return $url_string;
		
	}
	
	public  function ajaxarmory(){
		$name_arr = $_POST['name_arr'];
		$u_result = array_unique($name_arr);
		//dump($u_result);
		$u_str = implode("','",$u_result);
		$char_arr = d('character')->where("acc_name in ('".$u_str."') and Signature = 1")->select();
		foreach ($char_arr as $char){
			$xml = $this->getXML( $char['server'], $char['char_name'] );
			$c_array = (array)$xml->characterInfo->character;
			$char_result[$char['acc_name']] = array_merge($c_array['@attributes'],array('Signature'=>$char['Signature']));
			
			foreach($xml->characterInfo->characterTab->items->item as $v){
				$item = (array)$v;
				$item_arr['id'] = $item["@attributes"]['id'];
				$item_arr['icon'] = $item["@attributes"]['icon'];				
				$item_arr['backimage'] = 'http://db.178.com/wow/icons/m/' . $item["@attributes"]['icon'] . '.jpg';
				$char_result[$char['acc_name']][$c_array['@attributes']['name']][] = $item_arr;
				$item_arr = "";
			}			
		}
		$this->ajaxReturn($char_result,'',1);				
			
	}

	public function jion(){
		$this->chk();
		$acc_name = $_GET['acc_name'];
		if(d('guild_member')->where(array('acc_name'=>$acc_name))->find()){
			$this->assign('jumpUrl',"__URL__/ucenter"); 
			$this->error("已经加入工会了！");
			exit;
		}
		if(!d('character')->where(array('acc_name'=>$acc_name,'guild'=>"银月之城"))->count()){
			$this->assign('jumpUrl',"__URL__/ucenter"); 
			$this->error("请至少绑定一个属于 >>>银月之城<<< 的wow角色！");
			exit;
		}
		$member['acc_name'] = $acc_name;
		$member['guild'] = "银月之城";
		$member['status'] = '1';	
		$member['flag'] = '1';	
	
		if(d('guild_member')->add($member)){
			
			$this->assign('waitSecond',"");
			$this->assign('jumpUrl',"__URL__/ucenter"); 
			$this->success("申请加入成功！");
			exit;
			
		}
	}
	
	public function gquit(){
		$this->chk();
		
		if($_GET['acc_name']){
			$acc_name = $_GET['acc_name'];
			$member = d('guild_member')->where(array('acc_name'=>$acc_name,'guild'=>"银月之城",'status'=>1))->find();
			if($member){
				if($member['glevel'] == "会长"){
					$this->assign('jumpUrl',"__URL__/ucenter"); 
					$this->error("会长不能退出公会！");
					exit;
				}
				else{
					d('guild_member')->where(array('acc_name'=>$acc_name))->delete();
					$this->assign('jumpUrl',"__URL__/ucenter"); 
					$this->success("你已经退出了 >>>银月之城<<< ！");
					
					
				}
			}
			else{
				$this->assign('jumpUrl',"__URL__/ucenter"); 
				$this->error("你不是 >>>银月之城<<< 的一员 ！");
			}
		}
	}
	
	public function gchk(){
		$name = Cookie::get('acc_name');
		if($name){
			$join_menu = '<a href="__URL__/jion/acc_name/'.$name.'">申请加入公会</a>';	
			$g_member = d('guild_member')->where(array('acc_name' =>$name))->find();
			
			if($g_member['status']=="1" && $g_member['flag']=="1" ){
				$join_menu = '<a href="javascript:void(0)" onclick="'.'gquit(\''.$name.'\')" >退出公会</a>';
			}
			$this->assign('join_menu',$join_menu);
		}
		
	}

}
?>